package com.njth.servlet.action;

/*
 *author:陈帅
 *date: 3-21   
 * */
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.njth.beans.News;
import com.njth.dao.DaoConfig;
import com.njth.dao.NewsDAO;
import com.njth.util.Config;
import com.njth.util.FileAssistant;

public class AddNews extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public AddNews() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		NewsDAO newsdao = (NewsDAO) DaoConfig.getDao(NewsDAO.class);
		String path = Config.getConfigProperty("uploadImagePath");
		File uploadAbsolutDirectory = new File(Config.getContenAbsolutPath(),
				path);
		Map<String, Object> map = FileAssistant.fileUpload(request,
				uploadAbsolutDirectory);
		String ntitle = (String) map.get("newstitle");
		String context = (String) map.get("content");
		News news = new News();
		news.setTitle(ntitle);
		news.setContext(context);
		File newsimg = (File) map.get("file2");
		String newspic = newsimg.getName();
		news.setNewspic(newspic);
		Date date = new Date();
		news.setDatetime(date);
		newsdao.insert(news);
		ArrayList newsList = new ArrayList();
		newsList.add(news);
		ServletContext application=this.getServletContext(); 
		application.setAttribute("newsList", newsList);
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
